//
//  RCOrderManagerVC.m
//  RocketProject
//
//  Created by chen jiedan on 13-9-27.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCOrderManagerVC.h"
#import "RCBaseListDataModel.h"
#import "CommonDefines.h"
#import "RCServerOrderListCell.h"
#import "NSString+NSString_Ex_.h"
#import "RCServerCommentManageCell.h"
#import "RCServerImpressCell.h"
#import "RCServerActivityListCell.h"
#import "AppDelegate.h"
#import "RCAttendUserCell.h"
#import "RCOrderActivityManageDetailVC.h"
#import "RCActivityDetailVC.h"
#import "UIColor-Expanded.h"
#import "RCMessageCell.h"
#import "RCMessageDetailDialogVC.h"
#import "RCReplyCell.h"
#import "RCNotifyCell.h"


#define kSearchImageFile          @"image_search_searchBar.png"
#define kSearchIconImageFile      @"image_tabbar_search_normal.png"

#define kTextPlaceHolder          @"输入电话号码搜索"

@interface RCOrderManagerVC ()<IManagerDelegate,UITextFieldDelegate,RCServerStateChangeProtocol,RCActivityAttendProtocol>{
    UIImageView *_bgSelectedIndicator;
    
    UITextField    *_searchTextField;
    RCListType     _vcType;
    RCListReviewStatus   _status;
    
    RCBaseListDataModel *_model;
    int64_t  _reqID;
    BOOL     _refresh;
    int      _pn;
    
    
    BOOL    searchMode;
    RCBaseListDataModel *_filterModel;

    UITapGestureRecognizer *tap;

}

@property (nonatomic, copy) NSString *eventID;

@property (nonatomic, retain) RCBaseListDataModel *filterModel;
@property (nonatomic, retain) RCBaseListDataModel *model;
@property (nonatomic,retain) MBProgressHUDEx *progressHub;

@end

@implementation RCOrderManagerVC
@synthesize eventID = _eventID;
@synthesize model = _model;
@synthesize progressHub = _progressHub;
@synthesize filterModel = _filterModel;


- (id) initWithVCType:(RCListType)type withUniqueID:(NSString *)idString
{
    self = [super init];
    if (self) {
        _vcType = type;
        self.filterModel = [[[RCBaseListDataModel alloc] init] autorelease];
        self.filterModel.listItems = [[[NSMutableArray alloc] init] autorelease];
        self.eventID = idString ;
    }
    return self;
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.filterModel = [[[RCBaseListDataModel alloc] init] autorelease];
        self.filterModel.listItems = [[[NSMutableArray alloc] init] autorelease];
    }
    return self;
}


- (void) dealloc
{
    [[IManager sharedInstance] cancelFetchDataRequestByRequestID:_reqID];
    self.progressHub.delegate = nil;
    self.progressHub = nil;
    self.eventID = nil;
    KF_RELEASE_SAFELY(_model);
    [super dealloc];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    if (_vcType == RCListType_Order) {
        self.titleLabelText = @"订单管理";
    }else if (_vcType == RCListType_CommentManage){
        self.titleLabelText = @"评价管理";
    }else if (_vcType == RCListType_ImpressiveManage){
        self.titleLabelText = @"印象管理";
    }else if (_vcType == RCListType_ActivityManage){
        self.titleLabelText = @"活动管理";
    }else if (_vcType == RCListType_DetailActivityManage){
        self.titleLabelText = @"参与人数";
    }else if (_vcType == RCListType_MessageManage){
        self.titleLabelText = @"消息页面";
    }
    
    
    self.navigationItem.titleView = [self createTitleView];
    
    [self createStatusView];
    if (_vcType != RCListType_MessageManage) {
        [self createSearchView];
    }
    
    if ( baseVCTableView != nil ) {
        self.view.autoresizingMask |= UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
        //        baseVCTableView.autoresizingMask |= UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleBottomMargin;
        baseVCTableView.backgroundColor = [UIColor clearColor];
        baseVCTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        baseVCTableView.frame = self.view.bounds;
        CGRect tableViewNewFrame;
        if (_vcType != RCListType_MessageManage) {
            tableViewNewFrame = CGRectMake(baseVCTableView.frame.origin.x,baseVCTableView.frame.origin.y+40+50,baseVCTableView.frame.size.width,UIScreenHeight-44-40-20-50);
        }else{
            tableViewNewFrame = CGRectMake(baseVCTableView.frame.origin.x,baseVCTableView.frame.origin.y+50,baseVCTableView.frame.size.width,UIScreenHeight-44-50-20);
        }
        baseVCTableView.frame = tableViewNewFrame;
        baseVCTableView.tableHeaderView = nil;
        baseVCTableView.tableFooterView = nil;
        [self addPullRefresh];
    }
    [self refreshData];
}

- (void) createStatusView
{
    UIView *statusView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    [self.viewContainer addSubview:statusView];
    [statusView setBackgroundColor:[UIColor colorWithRed:230.0/255 green:230.0/255 blue:230.0/255 alpha:1]];
    [statusView release];
    
    if (_vcType == RCListType_ActivityManage) {
        for (int i = 0; i < 2; i ++) {
            UIButton *t = [[UIButton alloc] initWithFrame:CGRectMake(50+120*i, 8, 65, 30)];
            t.layer.masksToBounds = YES;
            t.layer.cornerRadius = 3.0f;
            [statusView addSubview:t];
            [t addTarget:self action:@selector(statusChanged:) forControlEvents:UIControlEventTouchUpInside];
            t.tag = 100+i;
            if (i == 0) {
                [t setBackgroundColor:[UIColor colorWithHex:0xffbdbbbb]];
                [t setTitle:@"正在优惠" forState:UIControlStateNormal];
            }else if (i == 1){
                [t setTitle:@"过期优惠" forState:UIControlStateNormal];
            }
            t.titleLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:14];
            [t setTitleColor:[UIColor colorWithHex:0xff505050] forState:UIControlStateNormal];
            //        [t setBackgroundColor:[UIColor yellowColor]];
            [t release];
        }
    }else if (_vcType == RCListType_MessageManage) {
        for (int i = 0; i < 3; i ++) {
            
            
            UIButton *t = [[UIButton alloc] initWithFrame:CGRectMake(20+105*i, 8, 70, 30)];
            t.layer.masksToBounds = YES;
            t.layer.cornerRadius = 3.0f;
            [statusView addSubview:t];
            [t addTarget:self action:@selector(statusChanged:) forControlEvents:UIControlEventTouchUpInside];
            t.tag = 100+i;
            
            UIImageView *middleLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, 49, 320, 1)];
            [middleLine setImage:[UIImage imageNamed:@"bg_middleLine"]];
            
            
            if (i == 0) {
//                [t setBackgroundColor:[UIColor colorWithHex:0xffbdbbbb]];
                [t setTitleColor:kNormalOrangeTextColor forState:UIControlStateNormal];
                [t setTitle:@"通知" forState:UIControlStateNormal];
                middleLine.frame = CGRectMake(105, -1, 1, 50);
                [statusView addSubview:middleLine];

            }else if (i == 1){
                [t setTitleColor:[UIColor colorWithHex:0xff505050] forState:UIControlStateNormal];
                [t setTitle:@"私信" forState:UIControlStateNormal];
                middleLine.frame = CGRectMake(210, -1, 1, 50);
                [statusView addSubview:middleLine];

            }else if (i == 2){
                [t setTitleColor:[UIColor colorWithHex:0xff505050] forState:UIControlStateNormal];
                [t setTitle:@"回应" forState:UIControlStateNormal];
            }
            t.titleLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:14];
            //        [t setBackgroundColor:[UIColor yellowColor]];
            [t release];
            [middleLine release];

        }
        [statusView setBackgroundColor:[UIColor colorWithRed:244.0/255 green:244.0/255 blue:244.0/255 alpha:1]];
        UIImageView *bottomLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, 49, 320, 1)];
        [bottomLine setImage:[UIImage imageNamed:@"bg_bottomLine"]];
        [statusView addSubview:bottomLine];
        [bottomLine release];
        
        
        _bgSelectedIndicator = [[UIImageView alloc] initWithFrame:CGRectMake(50, 44-4, 9, 9)];
        [_bgSelectedIndicator setImage:[UIImage imageNamed:@"bg_selectedIndicator"]];
        [statusView addSubview:_bgSelectedIndicator];
        [_bgSelectedIndicator release];
     
        
    }
//    else  if (_vcType == RCListType_PhotoManage) {
//        for (int i = 0; i < 5; i ++) {
//            UIButton *t = [[UIButton alloc] initWithFrame:CGRectMake(20+60*i, 10, 50, 40)];
//            [statusView addSubview:t];
//            [t addTarget:self action:@selector(statusChanged:) forControlEvents:UIControlEventTouchUpInside];
//            t.tag = 100+i;
//            if (i == 0) {
//                [t setTitle:@"菜" forState:UIControlStateNormal];
//            }else if (i == 1){
//                [t setTitle:@"环境" forState:UIControlStateNormal];
//            }else if (i == 2) {
//                [t setTitle:@"价目表" forState:UIControlStateNormal];
//            }else if (i == 3){
//                [t setTitle:@"其他" forState:UIControlStateNormal];
//            }else if (i == 4){
//                [t setTitle:@"全部" forState:UIControlStateNormal];
//            }
//            t.titleLabel.font = [UIFont fontWithName:kFontNameHelvetica size:15];
//            [t setTitleColor:kNormalHeadTextColor forState:UIControlStateNormal];
//            //        [t setBackgroundColor:[UIColor yellowColor]];
//            [t release];
//        }
//    }
    else{
        for (int i = 0; i < 3; i ++) {
            UIButton *t = [[UIButton alloc] initWithFrame:CGRectMake(15+110*i, 8, 65, 30)];
            t.layer.masksToBounds = YES;
            t.layer.cornerRadius = 3.0f;
            [statusView addSubview:t];
            [t addTarget:self action:@selector(statusChanged:) forControlEvents:UIControlEventTouchUpInside];
            t.tag = 100+i;
            if (i == 0) {
                [t setBackgroundColor:[UIColor colorWithHex:0xffbdbbbb]];
                if (_vcType == RCListType_DetailActivityManage) {
                    [t setTitle:@"等待确认" forState:UIControlStateNormal];
                }else{
                    [t setTitle:@"待审核" forState:UIControlStateNormal];
                }
            }else if (i == 1){
                if (_vcType == RCListType_DetailActivityManage) {
                    [t setTitle:@"交易成功" forState:UIControlStateNormal];
                }else{
                    [t setTitle:@"审核通过" forState:UIControlStateNormal];
                }
                
            }else{
                if (_vcType == RCListType_DetailActivityManage) {
                    [t setTitle:@"交易失败" forState:UIControlStateNormal];
                }else{
                    [t setTitle:@"审核失败" forState:UIControlStateNormal];
                }
            }
            t.titleLabel.font = [UIFont fontWithName:kFontNameLightHeiti size:14];
            [t setTitleColor:[UIColor colorWithHex:0xff505050] forState:UIControlStateNormal];
            //        [t setBackgroundColor:[UIColor yellowColor]];
            [t release];
        }
    }
    
    UIImageView *lineImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 43, 320, 1)];
    lineImageView.backgroundColor = [UIColor clearColor];
    [lineImageView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image_order_line.png" ofType:nil]]];
    [statusView addSubview:lineImageView];
    [lineImageView release];
}

- (void) createSearchView
{
    UIImageView *searchImageView = [[UIImageView alloc]initWithFrame:CGRectMake(14, 3+50, 292, 30)];
    searchImageView.userInteractionEnabled = YES;
    searchImageView.backgroundColor = [UIColor clearColor];
    NSString *searchpath = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:kSearchImageFile];
    UIImage *searchImage = [UIImage imageWithContentsOfFile:searchpath];
    searchImage = [searchImage resizableImageWithCapInsets:UIEdgeInsetsMake(10, 50, 10, 50)];
    [searchImageView setImage:searchImage];
    
    UIImageView *searchIconImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 8, 18, 18)];
    searchIconImageView.backgroundColor = [UIColor clearColor];
    NSString *searchIconpath = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:kSearchIconImageFile];
    [searchIconImageView setImage:[UIImage imageWithContentsOfFile:searchIconpath]];
    [searchImageView addSubview:searchIconImageView];
    [searchIconImageView release];
    
    if (!_searchTextField) {
        _searchTextField = [[UITextField alloc] initWithFrame:CGRectMake(30, 5, 280, 24)];
        _searchTextField.borderStyle = UITextBorderStyleNone;
        _searchTextField.delegate = self;
        if (_vcType == RCListType_Order) {
            _searchTextField.placeholder = kTextPlaceHolder;
        }else if (_vcType == RCListType_ActivityManage){
            _searchTextField.placeholder = @"输入活动主题搜索";
        }else if (_vcType == RCListType_DetailActivityManage){
            _searchTextField.placeholder = kTextPlaceHolder;
        }else if (_vcType == RCListType_ImpressiveManage){
            _searchTextField.placeholder = @"输入用户名搜索";
        }else if (_vcType == RCListType_CommentManage){
            _searchTextField.placeholder = @"输入用户名搜索";
        }
        _searchTextField.font = [UIFont fontWithName:kFontNameLightHeiti size:12];
        _searchTextField.textAlignment = UITextAlignmentLeft;
        _searchTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        _searchTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _searchTextField.autocorrectionType = UITextAutocorrectionTypeNo;
        _searchTextField.autocapitalizationType = UITextAutocapitalizationTypeNone;
        _searchTextField.returnKeyType = UIReturnKeySearch;
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(textFieldDidChange:)
                                                     name:UITextFieldTextDidChangeNotification
                                                   object:nil];
    }
    
    [searchImageView addSubview:_searchTextField];
    [searchImageView bringSubviewToFront:_searchTextField];
    
    [self.viewContainer addSubview:searchImageView];
    [searchImageView release];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



//切换的时候刷新数据

- (void) statusChanged:(id)sender
{
    [_searchTextField resignFirstResponder];
    _searchTextField.text = nil;
    searchMode = NO;
    
    UIButton *t = (UIButton *)sender;
    if (_vcType == RCListType_MessageManage) {
        [t setTitleColor:[UIColor colorWithHex:0xffff6800] forState:UIControlStateNormal];
    }else{
        [t setBackgroundColor:[UIColor colorWithHex:0xffbdbbbb]];
    }
    
    UIView *superView = t.superview;
    for (UIView *sub in superView.subviews) {
        if ([sub isKindOfClass:[UIButton class]]) {
            if (sub != t) {
                if (RCListType_MessageManage == _vcType) {
                    [((UIButton *)sub) setTitleColor:[UIColor colorWithHex:0xff505050] forState:UIControlStateNormal];
                }
                [sub setBackgroundColor:[UIColor clearColor]];
            }
        }
    }
    _status = t.tag - 100;
    
    _bgSelectedIndicator.frame = CGRectMake(50+_status*105, 50-4, 9, 9);

    
    self.model = Nil;
    [self refreshData];
    
}


- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (searchMode) {
       return  [self.filterModel.listItems count];
    }else{
        return [self.model.listItems count];
    }
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    if (_vcType == RCListType_Order) {
        cell = [[[RCServerOrderListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"RCServerOrderListCell"] autorelease];
    }else if(_vcType == RCListType_CommentManage){
        cell = [[[RCServerCommentManageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"RCServerCommentManageCell"] autorelease];

    }else if(_vcType == RCListType_ImpressiveManage){
        cell = [[[RCServerImpressCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"RCServerImpressCell"] autorelease];
        
    }else if(_vcType == RCListType_ActivityManage){
        cell = [[[RCServerActivityListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"RCServerActivityListCell"] autorelease];
    }else if(_vcType == RCListType_DetailActivityManage){
        cell = [[[RCAttendUserCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"RCAttendUserCell"] autorelease];
    }else if(_vcType == RCListType_MessageManage){
        if (_status == RCListReviewStatus_UnderReview) {
            cell = [[[RCNotifyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"RCNotifyCell"] autorelease];
        }else if (_status == RCListReviewStatus_ReviewSuccess) {
            cell = [[[RCMessageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"RCMessageManageCell"] autorelease];
        }else{
            cell = [[[RCReplyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"RCReplyCell"] autorelease];
        }
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    
    NSObject *item = nil;
    if (searchMode) {
        item = [self.filterModel.listItems objectAtIndex:[indexPath row]];
    }else{
        item = [self.model.listItems objectAtIndex:[indexPath row]];
    }
    
    if (_vcType == RCListType_Order) {
        [(RCServerOrderListCell *)cell bindData:(RCServerOrderItem*)item];
    }else if (_vcType == RCListType_CommentManage){
        
        [(RCServerCommentManageCell *)cell setReviewStatus:_status];
        
        [(RCServerCommentManageCell *)cell bindData:(RCServerCommentManageItem*)item];
        [(RCServerCommentManageCell *)cell setDelegate:self];
    }else if (_vcType == RCListType_ImpressiveManage){
        
        [(RCServerImpressCell *)cell setReviewStatus:_status];
        
        [(RCServerImpressCell *)cell bindData:(RCImpressiveItemDataModel*)item];
        [(RCServerImpressCell *)cell setDelegate:self];
    }else if (_vcType == RCListType_ActivityManage){
        [(RCServerActivityListCell *)cell bindData:(RCServerActivityItemDataModel*)item];
        [(RCServerActivityListCell *)cell setDelegate:self];

    }else if (_vcType == RCListType_DetailActivityManage){
        [(RCAttendUserCell *)cell bindData:(RCAttendPersonItemDataModel*)item];
//        [(RCServerActivityListCell *)cell setDelegate:self];
        
    }else if (_vcType == RCListType_MessageManage){
        if (_status == RCListReviewStatus_UnderReview) {
            [(RCNotifyCell *)cell  bindData:(RCMessageNotifyListItemDataModel *)item];
        }else if (_status == RCListReviewStatus_ReviewSuccess){
            [(RCMessageCell *)cell  bindData:(RCMessageListItemDataModel *)item];
        }else{
            [(RCReplyCell *)cell  bindData:(RCMessageReplyListItemDataModel *)item];
        }
    }
    
    return cell;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSObject *item = nil;
    if (searchMode) {
        item = [self.filterModel.listItems objectAtIndex:[indexPath row]];
    }else{
        item = [self.model.listItems objectAtIndex:[indexPath row]];
    }
    
    if (_vcType == RCListType_CommentManage) {
        return [RCServerCommentManageCell cellHeightForData:(RCServerCommentManageItem *)item];
    }else if (_vcType == RCListType_ImpressiveManage){
        return [RCServerImpressCell cellHeight:(RCImpressiveItemDataModel *)item];
    }else if (_vcType == RCListType_ActivityManage){
        return 135;
    }else if (_vcType == RCListType_DetailActivityManage){
        return 70;
    }else if (_vcType == RCListType_Order){
        return 95;
    }else if (_vcType == RCListType_MessageManage){
        if (_status == RCListReviewStatus_UnderReview) {
            return [RCNotifyCell cellHeight:(RCMessageNotifyListItemDataModel*)item];

        }else if (_status == RCListReviewStatus_ReviewSuccess){
            return [RCMessageCell cellHeight:(RCMessageListItemDataModel*)item];
        }else{
            return [RCReplyCell cellHeight:(RCMessageReplyListItemDataModel*)item];

        }
    }
    return 90;
}


- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    if (_vcType == RCListType_DetailActivityManage ) {
        RCAttendPersonItemDataModel *item = nil;
        if (searchMode) {
            item = [self.filterModel.listItems objectAtIndex:[indexPath row]];
        }else{
            item = [self.model.listItems objectAtIndex:[indexPath row]];
        }
        RCOrderActivityManageDetailVC *vc = [[RCOrderActivityManageDetailVC alloc] initWithVCType:DetailVCType_ActivityManage withID:item.attendID withEventID:self.eventID];
        vc.state = _status;
        [self.navigationController pushViewController:vc animated:YES];
        [vc release];
    }else if ( _vcType == RCListType_Order){
        RCServerOrderItem *item = nil;
        if (searchMode) {
            item = [self.filterModel.listItems objectAtIndex:[indexPath row]];
        }else{
            item = [self.model.listItems objectAtIndex:[indexPath row]];
        }
        RCOrderActivityManageDetailVC *vc = [[RCOrderActivityManageDetailVC alloc] initWithVCType:DetailvcType_Order withID:item.orderid withEventID:self.eventID];
        vc.state = _status;
        [self.navigationController pushViewController:vc animated:YES];
        [vc release];
    }else if (_vcType == RCListType_ActivityManage){
        RCServerActivityItemDataModel *item = nil;
        if (searchMode) {
            item = [self.filterModel.listItems objectAtIndex:[indexPath row]];
        }else{
            item = [self.model.listItems objectAtIndex:[indexPath row]];
        }
        RCActivityDetailVC *vc = [[RCActivityDetailVC alloc] initWithItem:item];
        [self.navigationController pushViewController:vc animated:YES];
        [vc release];
        
    }else if (_vcType == RCListType_MessageManage){
        if (_status == RCListReviewStatus_ReviewSuccess || _status == RCListReviewStatus_ReviewFailed) {
            RCMessageListItemDataModel *item = nil;
            if (searchMode) {
                item = [self.filterModel.listItems objectAtIndex:[indexPath row]];
            }else{
                item = [self.model.listItems objectAtIndex:[indexPath row]];
            }
            RCMessageDetailDialogVC *vc = [[RCMessageDetailDialogVC alloc] initWithUserName:item.username];
            [self.navigationController pushViewController:vc animated:YES];
            [vc release];

        }
        
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark ---网络请求---
- (void) refreshData
{
    _progressHub = [[MBProgressHUDEx alloc] initWithView:self.view
                                               TopHeight:0
                                        withBottomHeight:0
                                           withLeftWidth:0
                                          withRightWidth:0
                                    withExclusiveObjects:nil];
    _progressHub.delegate = self;
    _progressHub.labelText = @"加载中...";
    [self.view addSubview:_progressHub];
    [self.view bringSubviewToFront:_progressHub];
    [_progressHub show:YES];
    _refresh = YES;
    _pn = 0;
    
    [[IManager sharedInstance] cancelFetchDataRequestByRequestID:_reqID];
    _reqID = [[IManager sharedInstance] requestListData:_vcType withStatus:_status withUniID:self.eventID withPn:0 withRn:10 withDelegate:self];
    
//    _reqID = [[IManager sharedInstance] requestListData:_vcType withStatus:_status withPn:0 withRn:10 withDelegate:self];
    
}

- (void) loadMoreData
{
    _refresh = NO;
    
    [[IManager sharedInstance] cancelFetchDataRequestByRequestID:_reqID];
    _reqID = [[IManager sharedInstance] requestListData:_vcType withStatus:_status withUniID:self.eventID withPn:_pn withRn:10 withDelegate:self];

//    _reqID = [[IManager sharedInstance] requestListData:_vcType withStatus:_status withPn:_pn withRn:10 withDelegate:self];
}


#pragma mark Imanager Delegate


- (void) updateViewForError:(NSError *)errorInfo withRequestId:(int64_t)requestId
{
    [_progressHub removeFromSuperview];
    [_progressHub release];
    _progressHub = nil;
    
    if (_reqID == requestId) {
        if (_refresh) {
            [self finishRefreshingData];
            _refresh = NO;
        }else{
            [self finishLoadMoreData];
        }
    }
    [RCAppDelegate alertWithCustomizeString:@"网络请求失败"];

}

- (void) updateViewForSuccess:(IDataModel *)dataModel withRequestId:(int64_t)requestId
{
    [_progressHub removeFromSuperview];
    [_progressHub release];
    _progressHub = nil;
    
    if (dataModel.errorNO == RCErrorTypeNoError) {
        RCBaseListDataModel *resultList = (RCBaseListDataModel *)dataModel;
        if (_refresh) {
            self.model = resultList;
            [self finishRefreshingData];
//            [baseVCTableView reloadData];
            _refresh = NO;
        }else{
            self.model.pn = resultList.pn;
            self.model.rn = resultList.rn;
            self.model.total = resultList.total;
            [self.model.listItems addObjectsFromArray:resultList.listItems];
            
            [baseVCTableView reloadData];
            [self finishLoadMoreData];
        }
        
        
        
        NSInteger currentTotal = [self.model.listItems count];
        _pn = currentTotal;
        
        if (currentTotal < self.model.total) {
            [self addPullLoadMore];
        }else{
            [self noMoreData];
        }
    }else{
        [RCAppDelegate alertWithCustomizeString:dataModel.errorMsg];
    }
    [baseVCTableView reloadData];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
//    searchMode = YES;
//    [self noMoreData];
    tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapOnTableView:)];
    [baseVCTableView addGestureRecognizer:tap];
    [tap release];
    
}

-(void) didTapOnTableView:(UIGestureRecognizer*) recognizer {
    [_searchTextField resignFirstResponder];
    if (tap != nil) {
        [baseVCTableView removeGestureRecognizer:tap];
        tap = nil;
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (tap != nil) {
        [baseVCTableView removeGestureRecognizer:tap];
        tap = nil;
    }
    return YES;
}


- (void)textFieldDidChange :(NSNotification *)notif
{
    //
    NSString *inputStr = [_searchTextField text];
    
    if ([NSString stringIsNullOrEmpty:inputStr]) {
        searchMode = NO;
    }else{
        searchMode = YES;
        [self.filterModel.listItems removeAllObjects];
        for (RCServerOrderItem *item in self.model.listItems) {
            NSString *content = nil;
            if ([item isKindOfClass:[RCImpressiveItemDataModel class]]) {
                content = ((RCImpressiveItemDataModel*)item).userName;
            }else if ([item isKindOfClass:[RCServerActivityItemDataModel class]]) {//活动主题
                content = ((RCServerActivityItemDataModel*)item).title;
            }else if ([item isKindOfClass:[RCServerCommentManageItem class]]) { //评价管理
                content = ((RCServerCommentManageItem*)item).userName;
            }else if ([item isKindOfClass:[RCServerOrderItem class]]) { //订单管理
                content = ((RCServerOrderItem*)item).phone;
            }else if ([item isKindOfClass:[RCAttendPersonItemDataModel class]]) { //具体某一个活动管理
                content = ((RCAttendPersonItemDataModel*)item).phone;
            }else{
//                content = item.phone;
            }
            
            NSRange range = [content rangeOfString:inputStr options:NSCaseInsensitiveSearch];
            if (range.location != NSNotFound && range.length != 0) {
                [self.filterModel.listItems addObject:item];
            }
        }
        self.filterModel.total = [self.filterModel.listItems count];
    }
    [baseVCTableView reloadData];
}


//通过 不通过的回调
- (void) didStateChange:(BOOL)success withMsg:(NSString *)message
{
    if (success) {
        [self refreshData];
    }else{
        [RCAppDelegate alertWithCustomizeString:message];
    }
    
}

//参加活动 点击回调
- (void) didClickAtActivity:(NSString *)activityID
{
    if (activityID == nil) {
        [RCAppDelegate alertWithCustomizeString:@"暂无人员参加"];
    }else{
        RCOrderManagerVC *vc = [[RCOrderManagerVC alloc] initWithVCType:RCListType_DetailActivityManage withUniqueID:activityID];
        [self.navigationController pushViewController:vc animated:YES];
        [vc release];
    }
}

- (void) scrollViewDidScroll:(UIScrollView *)scrollView
{
    [_searchTextField resignFirstResponder];
}

@end
